<?php defined('SYSPATH') or die('No direct script access.');

class Controller_BoReport extends Controller_BoBase {
	public $auth_required = TRUE;

	public function action_index()
	{
		$view = View::factory("pages/bo/home");
		$this->setTab($view);
		$this->baseInfo($view);
		$this->request->response = $view;
	}

	public function action_search(){
		$view = View::factory("pages/bo/report/report_page");

		$search_result_startdate = null;
		$search_result_enddate = null;
		
		$scanSql = "select t.desc,count(t.desc) as county from transaction_history t 
					where t.type =  'scan' ";
		
		$searchSql = "select t.desc,count(t.desc) as county from transaction_history t 
					where t.type =  'search'";
		
		if(array_key_exists("startdate", $_REQUEST) && !empty($_REQUEST["startdate"])){
			$scanSql = $scanSql." and t.createtime > '".$_REQUEST["startdate"]."'";
			$searchSql = $searchSql." and t.createtime > '".$_REQUEST["startdate"]."'";
			$search_result_startdate = $_REQUEST["startdate"];
		}
		if(array_key_exists("enddate", $_REQUEST) && !empty($_REQUEST["enddate"])){
			$scanSql = $scanSql." and t.createtime < '".$_REQUEST["enddate"]."'";
			$searchSql = $searchSql." and t.createtime < '".$_REQUEST["enddate"]."'";
			$search_result_enddate = $_REQUEST["enddate"];
		}
		$scanSql = $scanSql." group by t.desc order by county desc limit 0,30";
		$searchSql = $searchSql." group by t.desc order by county desc limit 0,30";
		
		$scan_result = DB::query(Database::SELECT, $scanSql)->execute();
		$search_result = DB::query(Database::SELECT, $searchSql)->execute();
		$this->setTab($view);
		$view->set_global('scan_result', $scan_result);
		$view->set_global('search_result', $search_result);
		$view->set_global('search_result_startdate', $search_result_startdate);
		$view->set_global('search_result_enddate', $search_result_enddate);
		exit($view->render());
	}

	private function baseInfo($view){
		$scanSql = "select t.desc,count(t.desc) as county from transaction_history t 
					where t.type =  'scan' 
					group by t.desc 
					order by county desc
					limit 0,30";
		
		$searchSql = "select t.desc,count(t.desc) as county from transaction_history t 
					where t.type =  'search' 
					group by t.desc 
					order by county desc
					limit 0,30";
		
		$scan_result = DB::query(Database::SELECT, $scanSql)->execute();
		$search_result = DB::query(Database::SELECT, $searchSql)->execute();
		
		$view->set_global('scan_result', $scan_result);
		$view->set_global('search_result', $search_result);
	}

}
